package com.llap.exp190929.service;

import com.llap.exp190929.entity.UserType;
import com.llap.exp190929.repository.UserTypeRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@AllArgsConstructor
@Slf4j
public class UserTypeService {
    private UserTypeRepository userTypeRepository;

    public UserType findUserType(int id) {
        return userTypeRepository.findById(id).orElse(null);
    }

    public List<UserType> findAll() {
        return userTypeRepository.findAll();
    }

    public Page<UserType> findUserTypes(int page, int pageSize) {
        return userTypeRepository.findAll(PageRequest.of(page - 1, pageSize));
    }

    @Transactional
    public UserType addUserType(UserType usertype) {
        if (usertype.getId() != null) return null;
        return userTypeRepository.save(usertype);
    }

    @Transactional
    public UserType updateUserType(UserType usertype) {
        if (usertype.getId() == null) return null;
        return userTypeRepository.save(usertype);
    }

    @Transactional
    public void deleteUserType(int id) {
        userTypeRepository.deleteById(id);
    }
}
